﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using GPS.GPS_DAL;
using System.Web.Script.Serialization;

namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_DiemTotNghiepController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }
        #region Index

        /// <summary>
        /// Index
        /// </summary>
        /// <param name="SearchModel"></param>
        /// <param name="currentPage"></param>
        /// <returns></returns>
        public ActionResult Index(ViewModelDDTTN_KQTN SearchModel, int? currentPage)
        {
            SearchModel.pDVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            initialCategorySearchAction(SearchModel);
            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GETDS_KQTN_HOCVIEN(SearchModel.pDVGTVT_MA
                                                                                               , SearchModel.pKH_MA
                                                                                               , SearchModel.pHV_MA
                                                                                               , SearchModel.pHV_HOTEN
                                                                                               , SearchModel.pHV_CMND
                                                                                               , SearchModel.pHANG_DT
                                                                                               , SearchModel.pSO_QD_TN
                                                                                              );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }

        /// <summary>
        /// Search
        /// </summary>
        /// <param name="SearchModel"></param>
        /// <param name="currentPage"></param>
        /// <returns></returns>
        public ActionResult Search(ViewModelDDTTN_KQTN SearchModel, int? currentPage)
        {
            //if (SearchModel.pDVGTVT_MA == null || string.IsNullOrWhiteSpace(SearchModel.pDVGTVT_MA))
            //    SearchModel.pDVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            if (SearchModel.pDVGTVT_MA == null)
                SearchModel.pDVGTVT_MA = "40";

            string _DVGTGT = this.getCurrentUser().MA_TINHTP;
            //    if (SearchModel.pDVGTVT_MA !="40")

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            // var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.pDVGTVT_MA);
            //  ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_TC(SearchModel.pDVGTVT_MA, SearchModel.pKH_MA); // _db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.pDVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            ViewBag.HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(o => o.HANGDT_RANK > 4), "HANGDT_MA", "HANGDT_TEN");

            ViewBag.DTTN_QDs = new SelectList(_db.Filter<DDTTN_QD>(o => o.DDTTN_QD_DVGTVT_MA == SearchModel.pDVGTVT_MA && o.DDTTN_QD_TT == 1), "ID", "DDTTN_QD_TENKHOA");

            var entities = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GETDS_KQTN_HOCVIEN_S(SearchModel.pDVGTVT_MA
                                                                                               , SearchModel.pKH_MA
                                                                                               , SearchModel.pHV_MA
                                                                                               , SearchModel.pHV_HOTEN
                                                                                               , SearchModel.pHV_CMND
                                                                                               , SearchModel.pHANG_DT
                                                                                               , SearchModel.pSO_QD_TN
                                                                                               , SearchModel.pNGAY_KQTN_TUNGAY
                                                                                               , SearchModel.pNGAY_KQTN_DENNGAY
                                                                                              );

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = entities.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);

            ViewBag.TotalRow = entities.Count();

            return View(SearchModel);
        }
        #region"Ajax"
        [HttpPost]
        public ActionResult getKH_MAs(string ma_DVGT)
        {
            var districts = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN(ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_TEN + "</option>");
            var khoatotnghiep = _db.Filter<DDTTN_QD>(o => ((ma_DVGT == null || ma_DVGT == "40") || o.DDTTN_QD_DVGTVT_MA == ma_DVGT)).Select(a => "<option value='" + a.ID + "'>" + a.DDTTN_QD_TENKHOA + "</option>");
            var content = string.Join("", districts) + "#" + string.Join("", khoatotnghiep);
            return Content(content);
        }




        #endregion


        public void initialCategorySearchAction(ViewModelDDTTN_KQTN viewModel)
        {
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MA == viewModel.pDVGTVT_MA && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;

            //KH_MAs
            // var khoahocs = _db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.pDVGTVT_MA);
            //  ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_TN(viewModel.pDVGTVT_MA, viewModel.pKH_MA); // _db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.pDVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");

            ViewBag.HANGDT_MAs = new SelectList(_db.Filter<SHANGDT>(o => o.HANGDT_RANK > 4), "HANGDT_MA", "HANGDT_TEN");

            ViewBag.DTTN_QDs = new SelectList(_db.Filter<DDTTN_QD>(o => o.DDTTN_QD_DVGTVT_MA == viewModel.pDVGTVT_MA && o.DDTTN_QD_TT == 1), "ID", "DDTTN_QD_TENKHOA");

        }
        #endregion

        #region Create
        public ActionResult Create(string id1, string id2, decimal? id3, string id4, decimal? id5, string id6)
        {
            string HV_MA = id4;
            string HV_DVGTVT = id1;
            string KH_MA = id2;
            decimal? DDTN_ID = id3;
            decimal? DTTN_QD_ID = id5;

            DHV _hocvien = _db.GetOne<DHV>(o => o.HV_MA == HV_MA && o.HV_DVGTVT_MA == HV_DVGTVT);
            CrudModelDDTTN_KQTN _model = new CrudModelDDTTN_KQTN();
            _model.DTTN_KQTN_DVGTVT_MA = _hocvien.HV_DVGTVT_MA;
            _model.DTTN_KQTN_HV_MA = _hocvien.HV_MA;
            //_model.DTTN_KQTN_KH_LOP_MA = _hocvien.HV_KH_LOP_MA;
            _model.DTTN_KQTN_KH_MA = _hocvien.HV_KH_MA;
            _model.DTTN_KQTN_DTTN_ID = DDTN_ID.Value;
            _model.DTTN_KQTN_DTTN_QD_ID = DTTN_QD_ID.Value;
            _model.DTTN_HGPLX_MA = id6;
            ViewBag.SSHVs = new SelectList(_db.GetAll<SSHV>(), "SHV_MA", "SHV_HOTEN");
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MA == Ma_GTVT && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            ViewBag.TEN_HV = _hocvien.HV_HODEM + " " + _hocvien.HV_TEN;

            return View(_model);
        }

        [HttpPost]
        public ActionResult Create(CrudModelDDTTN_KQTN model)
        {
            if (ModelState.IsValid)
            {
                DDTTN_KQTN _model = new DDTTN_KQTN();
                GPS.COMMON.Helpers.CopyObject<DDTTN_KQTN>(model, ref _model, new List<string> { "DTTN_KQTN_XH" });
                _model.DTTN_KQTN_NGAYNHAP = DateTime.Now;
                _model.DTTN_KQTN_NGUOINHAP = User.Identity.Name;
                _model.ID = EFSequence.GetNextValue("SEQ_DDTTN_KQTN_ID");

                var _tt = _db.GetOne<DDTTN>(o => o.ID == _model.DTTN_KQTN_DTTN_ID);
                if (_tt != null && !string.IsNullOrWhiteSpace(_tt.DTTN_TT))
                    _model.DTTN_KQTN_TT = _tt.DTTN_TT;
                else
                    _model.DTTN_KQTN_TT = "MOI";

                //Tính toán xếp hạng:
                List<SXEPHANG> lstXepHang = _db.Filter<SXEPHANG>(o => o.XEPHANG_HANGGPLX_MA == model.DTTN_HGPLX_MA);

                //check vang Thi tot nghiep
                if (model.DTTN_KQTN_LT == null)
                    model.DTTN_KQTN_VANG_LT = "1";

                if (model.DTTN_KQTN_TH == null)
                    model.DTTN_KQTN_VANG_TH = "1";

                if (model.DTTN_KQTN_TD == null)
                    model.DTTN_KQTN_VANG_TD = "1";

                if (model.DTTN_KQTN_CC == null)
                    model.DTTN_KQTN_VANG_CC = "1";

                //them vào 2017-08-20
                if (model.DTTN_KQTN_TBC == null)
                    model.DTTN_KQTN_TBC = 0;

                if (model.DTTN_KQTN_VANG_LT == "1")
                    _model.DTTN_KQTN_LT = 0;
                if (model.DTTN_KQTN_VANG_TH == "1")
                    _model.DTTN_KQTN_TH = 0;
                if (model.DTTN_KQTN_VANG_TD == "1")
                    _model.DTTN_KQTN_TD = 0;
                if (model.DTTN_KQTN_VANG_CC == "1")
                    _model.DTTN_KQTN_CC = 0;


                if (lstXepHang != null && lstXepHang.Count > 0)
                {
                    int k = 0;
                    for (int i = 0; i < lstXepHang.Count; i++)
                    {
                        if (lstXepHang[i].XEPHANG_DIEMTB_DEN > model.DTTN_KQTN_TKKH)//sửa 2017-08-20
                        {
                            _model.DTTN_KQTN_ID_XEPHANG = lstXepHang[i].ID;
                            k = i;
                            break;
                        }

                    }
                    if(k==0)
                    {
                       k= lstXepHang.Count - 1;
                       _model.DTTN_KQTN_ID_XEPHANG = lstXepHang[k].ID;
                    }
                    //Kiểm tra chi tiết từng nội dung thi:
                    if (_model.DTTN_KQTN_LT < lstXepHang[0].XEPHANG_LTMIN)
                    {
                        //|| _model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN)
                        //không đạt
                        _model.DTTN_KQTN_TTTN = "TLT";
                    }
                    else if (_model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN)
                    {
                        _model.DTTN_KQTN_TTTN = "TTH";
                    }
                    else if (_model.DTTN_KQTN_LT < lstXepHang[0].XEPHANG_LTMIN && (_model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN))
                    {
                        _model.DTTN_KQTN_TTTN = "TTN";
                    }

                    if (_model.DTTN_KQTN_LT >= lstXepHang[0].XEPHANG_LTMIN && _model.DTTN_KQTN_TD >= lstXepHang[0].XEPHANG_TDMIN
                              && _model.DTTN_KQTN_TH >= lstXepHang[0].XEPHANG_THMIN && _model.DTTN_KQTN_CC >= lstXepHang[0].XEPHANG_CCMIN
                              && lstXepHang[k].XEPHANG_ACTIVE == "1")
                    {
                        _model.DTTN_KQTN_TTTN = "DAT";
                    }

                    if (model.DTTN_KQTN_VANG_LT == "1")
                        _model.DTTN_KQTN_LT = null;
                    if (model.DTTN_KQTN_VANG_TH == "1")
                        _model.DTTN_KQTN_TH = null;
                    if (model.DTTN_KQTN_VANG_TD == "1")
                        _model.DTTN_KQTN_TD = null;
                    if (model.DTTN_KQTN_VANG_CC == "1")
                        _model.DTTN_KQTN_CC = null;

                }



                //END

                //Begin transaction
                //   _db.BeginTransaction();

                _db.Insert<DDTTN_KQTN>(_model);
                //update DHV_HS:

                var _DHV_HS = _db.GetOne<DHV_HS>(o => o.HV_HS_DVGTVT_MA == model.DTTN_KQTN_DVGTVT_MA && o.HV_HS_HV_MA == model.DTTN_KQTN_HV_MA);
                if (_DHV_HS != null)
                {
                    //_DHV_HS.HV_HS_KQTN_VANG_LT = model.DTTN_KQTN_VANG_LT;
                    //_DHV_HS.HV_HS_KQTN_LT = model.DTTN_KQTN_LT;

                    //_DHV_HS.HV_HS_KQTN_VANG_TH = model.DTTN_KQTN_VANG_TH;
                    //_DHV_HS.HV_HS_KQTN_TH = model.DTTN_KQTN_TH;

                    //_DHV_HS.HV_HS_KQTN_VANG_TD = model.DTTN_KQTN_VANG_TD;
                    //_DHV_HS.HV_HS_KQTN_TD = model.DTTN_KQTN_TD;

                    //_DHV_HS.HV_HS_KQTN_VANG_CC = model.DTTN_KQTN_VANG_CC;
                    //_DHV_HS.HV_HS_KQTN_CC = model.DTTN_KQTN_CC;

                    //_DHV_HS.HV_HS_KQTN_TONG = model.DTTN_KQTN_TONG;
                    //_DHV_HS.HV_HS_KQTN_TB = model.DTTN_KQTN_TB;

                    _DHV_HS.HV_HS_TT = _model.DTTN_KQTN_TTTN;
                    _db.Update<DHV_HS>(_DHV_HS);
                }

                //_db.DbContext.DHV_HS.Attach(_person);
                //_db.DbContext.Entry(_person).Property(X => X.HV_HS_XEPLOAITN).IsModified = true;
                //_db.DbContext.SaveChanges();

                //_db.CommitTransaction();
                //Commit transaction

                //return RedirectToAction("Index");
                //string url = Url.Action("Index", "GPS_DiemTotNghiep");

                //return Json(new { success = true, url = url });
                return RedirectToAction("Index");
            }

            return View(model);
        }

        #endregion

        #region Edit
        public ActionResult Edit(decimal id, string id6)
        {

            DDTTN_KQTN model = _db.GetOne<DDTTN_KQTN>(o => o.ID == id);// await _db.GetDbSet<DDTTN_KQTN>().FindAsync(id);
            CrudModelDDTTN_KQTN _model = new CrudModelDDTTN_KQTN();
            GPS.COMMON.Helpers.CopyObject<CrudModelDDTTN_KQTN>(model, ref _model, new List<string> { "DTTN_KQTN_XH" });
            //_model.DTTN_KQTN_XH = (model.DTTN_KQTN_XH == false ? "0" : "1");
            _model.DTTN_HGPLX_MA = id6;
            ViewBag.SSHVs = new SelectList(_db.GetAll<SSHV>(), "SHV_MA", "SHV_HOTEN");
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MA == Ma_GTVT && o.DVGTVT_ACTIVE == "1"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.DEFAULT_VALUE_DDL = CategoryCommon.DEFAULT_VALUE_DDL;
            string HV_MA = model.DTTN_KQTN_HV_MA;
            string HV_DVGTVT = model.DTTN_KQTN_DVGTVT_MA;
            DHV _hocvien = _db.GetOne<DHV>(o => o.HV_MA == HV_MA && o.HV_DVGTVT_MA == HV_DVGTVT);
            ViewBag.TEN_HV = _hocvien.HV_HODEM + " " + _hocvien.HV_TEN;

            return View(_model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDDTTN_KQTN model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<DDTTN_KQTN>(o => o.ID == model.ID);

                GPS.COMMON.Helpers.CopyObject<DDTTN_KQTN>(model, ref _model, new List<string> { "DTTN_KQTN_NGAYNHAP", "DTTN_KQTN_NGUOINHAP", "DTTN_KQTN_XH", "DTTN_KQTN_TT" });
                _model.DTTN_KQTN_NGAYSUA = DateTime.Now;
                _model.DTTN_KQTN_NGUOISUA = User.Identity.Name;
                //_model.DTTN_KQTN_XH = (model.DTTN_KQTN_XH == "0" ? false : true);

                var _tt = _db.GetOne<DDTTN>(o => o.ID == _model.DTTN_KQTN_DTTN_ID);
                if (_tt != null && !string.IsNullOrWhiteSpace(_tt.DTTN_TT))
                    _model.DTTN_KQTN_TT = _tt.DTTN_TT;
                else
                    _model.DTTN_KQTN_TT = "MOI";

                //check vang Thi tot nghiep
                if (model.DTTN_KQTN_LT == null)
                    model.DTTN_KQTN_VANG_LT = "1";

                if (model.DTTN_KQTN_TH == null)
                    model.DTTN_KQTN_VANG_TH = "1";

                if (model.DTTN_KQTN_TD == null)
                    model.DTTN_KQTN_VANG_TD = "1";

                if (model.DTTN_KQTN_CC == null)
                    model.DTTN_KQTN_VANG_CC = "1";

                //them vào 2017-08-20
                if (model.DTTN_KQTN_TBC == null)
                    model.DTTN_KQTN_TBC = 0;

                //Tính toán xếp hạng:
                if (model.DTTN_KQTN_VANG_LT == "1")
                    _model.DTTN_KQTN_LT = 0;
                if (model.DTTN_KQTN_VANG_TH == "1")
                    _model.DTTN_KQTN_TH = 0;
                if (model.DTTN_KQTN_VANG_TD == "1")
                    _model.DTTN_KQTN_TD = 0;
                if (model.DTTN_KQTN_VANG_CC == "1")
                    _model.DTTN_KQTN_CC = 0;

                List<SXEPHANG> lstXepHang = _db.Filter<SXEPHANG>(o => o.XEPHANG_HANGGPLX_MA == model.DTTN_HGPLX_MA);
                if (lstXepHang != null && lstXepHang.Count > 0)
                {
                    int k = 0;
                    for (int i = 0; i < lstXepHang.Count; i++)
                    {
                        if (lstXepHang[i].XEPHANG_DIEMTB_DEN > model.DTTN_KQTN_TKKH)//sửa 2017-08-20
                        {
                            _model.DTTN_KQTN_ID_XEPHANG = lstXepHang[i].ID;
                            k = i;
                            break;
                        }

                    }
                    if (k == 0)
                    {
                        k = lstXepHang.Count - 1;
                        _model.DTTN_KQTN_ID_XEPHANG = lstXepHang[k].ID;
                    }

                    //Kiểm tra chi tiết từng nội dung thi:
                    if (_model.DTTN_KQTN_LT < lstXepHang[0].XEPHANG_LTMIN && (_model.DTTN_KQTN_TD > lstXepHang[0].XEPHANG_TDMIN && _model.DTTN_KQTN_TH > lstXepHang[0].XEPHANG_THMIN && _model.DTTN_KQTN_CC > lstXepHang[0].XEPHANG_CCMIN))
                    {
                        _model.DTTN_KQTN_TTTN = "TLT";
                    }
                    else if (_model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN)
                    {
                        _model.DTTN_KQTN_TTTN = "TTH";
                    }
                    else if (_model.DTTN_KQTN_LT < lstXepHang[0].XEPHANG_LTMIN && (_model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN))
                    {
                        _model.DTTN_KQTN_TTTN = "TTN";
                    }

                    if (_model.DTTN_KQTN_LT >= lstXepHang[0].XEPHANG_LTMIN && _model.DTTN_KQTN_TD >= lstXepHang[0].XEPHANG_TDMIN
                              && _model.DTTN_KQTN_TH >= lstXepHang[0].XEPHANG_THMIN && _model.DTTN_KQTN_CC >= lstXepHang[0].XEPHANG_CCMIN
                              && lstXepHang[k].XEPHANG_ACTIVE == "1")
                    {
                        _model.DTTN_KQTN_TTTN = "DAT";
                    }

                    if (model.DTTN_KQTN_VANG_LT == "1")
                        _model.DTTN_KQTN_LT = null;
                    if (model.DTTN_KQTN_VANG_TH == "1")
                        _model.DTTN_KQTN_TH = null;
                    if (model.DTTN_KQTN_VANG_TD == "1")
                        _model.DTTN_KQTN_TD = null;
                    if (model.DTTN_KQTN_VANG_CC == "1")
                        _model.DTTN_KQTN_CC = null;

                }



                _db.Update<DDTTN_KQTN>(_model);

                //DHV_HS
                var _DHV_HS = _db.GetOne<DHV_HS>(o => o.HV_HS_DVGTVT_MA == model.DTTN_KQTN_DVGTVT_MA && o.HV_HS_HV_MA == model.DTTN_KQTN_HV_MA);
                if (_DHV_HS != null)
                {

                    //_DHV_HS.HV_HS_KQTN_VANG_LT = model.DTTN_KQTN_VANG_LT;
                    //_DHV_HS.HV_HS_KQTN_LT = model.DTTN_KQTN_LT;

                    //_DHV_HS.HV_HS_KQTN_VANG_TH = model.DTTN_KQTN_VANG_TH;
                    //_DHV_HS.HV_HS_KQTN_TH = model.DTTN_KQTN_TH;

                    //_DHV_HS.HV_HS_KQTN_VANG_TD = model.DTTN_KQTN_VANG_TD;
                    //_DHV_HS.HV_HS_KQTN_TD = model.DTTN_KQTN_TD;

                    //_DHV_HS.HV_HS_KQTN_VANG_CC = model.DTTN_KQTN_VANG_CC;
                    //_DHV_HS.HV_HS_KQTN_CC = model.DTTN_KQTN_CC;

                    //_DHV_HS.HV_HS_KQTN_TONG = model.DTTN_KQTN_TONG;
                    //_DHV_HS.HV_HS_KQTN_TB = model.DTTN_KQTN_TB;

                    _DHV_HS.HV_HS_TT = _model.DTTN_KQTN_TTTN;


                    _db.Update<DHV_HS>(_DHV_HS);
                }

                //return RedirectToAction("Index");
                //return Json(new { success = true, url = url });
                return RedirectToAction("Index");
            }

            return View(model);
        }


        #endregion

        #region Delete
        public ActionResult Delete(decimal id)
        {

            DDTTN_KQTN model = _db.GetOne<DDTTN_KQTN>(o => o.ID == id);
            _db.DeleteItem<DDTTN_KQTN>(model);

            //DHV_HS
            var _DHV_HS = _db.GetOne<DHV_HS>(o => o.HV_HS_DVGTVT_MA == model.DTTN_KQTN_DVGTVT_MA && o.HV_HS_HV_MA == model.DTTN_KQTN_HV_MA);
            _DHV_HS.HV_HS_XEPLOAITN = null;
            _db.Update<DHV_HS>(_DHV_HS);

            return RedirectToAction("Index");
        }
        #endregion


        public JsonResult InsertOrUpdate(string modeljson)
        {
            try
            {
                JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                CrudModelDDTTN_KQTN model = json_serializer.Deserialize<CrudModelDDTTN_KQTN>(modeljson);
                decimal? _ID = 0;
                if (model.ID == null) //Insert
                {
                    DDTTN_KQTN _model = new DDTTN_KQTN();
                    GPS.COMMON.Helpers.CopyObject<DDTTN_KQTN>(model, ref _model, new List<string> { "DTTN_KQTN_XH" });
                    _model.DTTN_KQTN_NGAYNHAP = DateTime.Now;
                    _model.DTTN_KQTN_NGUOINHAP = User.Identity.Name;
                    _model.ID = EFSequence.GetNextValue("SEQ_DDTTN_KQTN_ID");

                    var _tt = _db.GetOne<DDTTN>(o => o.ID == _model.DTTN_KQTN_DTTN_ID);
                    if (_tt != null && !string.IsNullOrWhiteSpace(_tt.DTTN_TT))
                        _model.DTTN_KQTN_TT = _tt.DTTN_TT;
                    else
                        _model.DTTN_KQTN_TT = "MOI";

                    //Tính toán xếp hạng:
                    List<SXEPHANG> lstXepHang = _db.Filter<SXEPHANG>(o => o.XEPHANG_HANGGPLX_MA == model.DTTN_HGPLX_MA);


                    //check vang Thi tot nghiep
                    if (model.DTTN_KQTN_LT == null)
                        model.DTTN_KQTN_VANG_LT = "1";

                    if (model.DTTN_KQTN_TH == null)
                        model.DTTN_KQTN_VANG_TH = "1";

                    if (model.DTTN_KQTN_TD == null)
                        model.DTTN_KQTN_VANG_TD = "1";

                    if (model.DTTN_KQTN_CC == null)
                        model.DTTN_KQTN_VANG_CC = "1";

                    //them vào 2017-08-20
                    if (model.DTTN_KQTN_TBC == null)
                        model.DTTN_KQTN_TBC = 0;


                    if (model.DTTN_KQTN_VANG_LT == "1")
                        _model.DTTN_KQTN_LT = 0;
                    if (model.DTTN_KQTN_VANG_TH == "1")
                        _model.DTTN_KQTN_TH = 0;
                    if (model.DTTN_KQTN_VANG_TD == "1")
                        _model.DTTN_KQTN_TD = 0;
                    if (model.DTTN_KQTN_VANG_CC == "1")
                        _model.DTTN_KQTN_CC = 0;




                    if (lstXepHang != null && lstXepHang.Count > 0)
                    {
                        int k = 0;
                        for (int i = 0; i < lstXepHang.Count; i++)
                        {
                            if (lstXepHang[i].XEPHANG_DIEMTB_DEN > model.DTTN_KQTN_TKKH) // sửa 2017-08-20
                            {
                                _model.DTTN_KQTN_ID_XEPHANG = lstXepHang[i].ID;
                                k = i;
                                break;
                            }

                        }
                        if (k == 0)
                        {
                            k = lstXepHang.Count - 1;
                            _model.DTTN_KQTN_ID_XEPHANG = lstXepHang[k].ID;
                        }


                        //Kiểm tra chi tiết từng nội dung thi:
                        if (_model.DTTN_KQTN_LT < lstXepHang[0].XEPHANG_LTMIN)
                        {
                            //|| _model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN)
                            //không đạt
                            _model.DTTN_KQTN_TTTN = "TLT";
                        }
                        else if (_model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN)
                        {
                            _model.DTTN_KQTN_TTTN = "TTH";
                        }
                        else if (_model.DTTN_KQTN_LT < lstXepHang[0].XEPHANG_LTMIN && (_model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN))
                        {
                            _model.DTTN_KQTN_TTTN = "TTN";
                        }

                        if (_model.DTTN_KQTN_LT >= lstXepHang[0].XEPHANG_LTMIN && _model.DTTN_KQTN_TD >= lstXepHang[0].XEPHANG_TDMIN
                              && _model.DTTN_KQTN_TH >= lstXepHang[0].XEPHANG_THMIN && _model.DTTN_KQTN_CC >= lstXepHang[0].XEPHANG_CCMIN
                              && lstXepHang[k].XEPHANG_ACTIVE == "1")
                        {
                            _model.DTTN_KQTN_TTTN = "DAT";
                        }

                        if (model.DTTN_KQTN_VANG_LT == "1")
                            _model.DTTN_KQTN_LT = null;
                        if (model.DTTN_KQTN_VANG_TH == "1")
                            _model.DTTN_KQTN_TH = null;
                        if (model.DTTN_KQTN_VANG_TD == "1")
                            _model.DTTN_KQTN_TD = null;
                        if (model.DTTN_KQTN_VANG_CC == "1")
                            _model.DTTN_KQTN_CC = null;
                    }


                    _db.Insert<DDTTN_KQTN>(_model);
                    _ID = _model.ID;
                    //update DHV_HS:

                    var _DHV_HS = _db.GetOne<DHV_HS>(o => o.HV_HS_DVGTVT_MA == model.DTTN_KQTN_DVGTVT_MA && o.HV_HS_HV_MA == model.DTTN_KQTN_HV_MA);
                    if (_DHV_HS != null)
                    {


                        _DHV_HS.HV_HS_TT = _model.DTTN_KQTN_TTTN;
                        _db.Update<DHV_HS>(_DHV_HS);
                    }
                }
                else //update
                {
                    _ID = model.ID;
                    var _model = _db.GetOne<DDTTN_KQTN>(o => o.ID == model.ID);
                    var lstExcept = new List<string> { "DTTN_KQTN_NGAYNHAP", "DTTN_KQTN_NGUOINHAP", "DTTN_KQTN_XH", "DTTN_KQTN_TT","DTTN_KQTN_DTTN_QD_ID","DTTN_KQTN_DTTN_ID","DTTN_KQTN_DVGTVT_MA"
                     ,"DTTN_KQTN_KH_MA","DTTN_KQTN_HV_MA","DTTN_KQTN_VANG_LT","DTTN_KQTN_LT_SHV_MA","DTTN_KQTN_NXSHV_LT","DTTN_KQTN_VANG_TH","DTTN_KQTN_VANG_TH","DTTN_KQTN_NXSHV_TH"
                     ,"DTTN_KQTN_VANG_TD","DTTN_KQTN_TD_SHV_MA","DTTN_KQTN_NXSHV_TD","DTTN_KQTN_VANG_CC","DTTN_KQTN_CC_SHV_MA","DTTN_KQTN_NXSHV_CC","DTTN_KQTN_MOTA"                  
                    };
                    GPS.COMMON.Helpers.CopyObject<DDTTN_KQTN>(model, ref _model, lstExcept);
                    _model.DTTN_KQTN_NGAYSUA = DateTime.Now;
                    _model.DTTN_KQTN_NGUOISUA = User.Identity.Name;
                    //_model.DTTN_KQTN_XH = (model.DTTN_KQTN_XH == "0" ? false : true);

                    var _tt = _db.GetOne<DDTTN>(o => o.ID == _model.DTTN_KQTN_DTTN_ID);
                    if (_tt != null && !string.IsNullOrWhiteSpace(_tt.DTTN_TT))
                        _model.DTTN_KQTN_TT = _tt.DTTN_TT;
                    else
                        _model.DTTN_KQTN_TT = "MOI";

                    //check vang Thi tot nghiep
                    if (model.DTTN_KQTN_LT == null)
                        model.DTTN_KQTN_VANG_LT = "1";

                    if (model.DTTN_KQTN_TH == null)
                        model.DTTN_KQTN_VANG_TH = "1";

                    if (model.DTTN_KQTN_TD == null)
                        model.DTTN_KQTN_VANG_TD = "1";

                    if (model.DTTN_KQTN_CC == null)
                        model.DTTN_KQTN_VANG_CC = "1";

                    //them vào 2017-08-20
                    if (model.DTTN_KQTN_TBC == null)
                        model.DTTN_KQTN_TBC = 0;

                    //Tính toán xếp hạng:
                    if (model.DTTN_KQTN_VANG_LT == "1")
                        _model.DTTN_KQTN_LT = 0;
                    if (model.DTTN_KQTN_VANG_TH == "1")
                        _model.DTTN_KQTN_TH = 0;
                    if (model.DTTN_KQTN_VANG_TD == "1")
                        _model.DTTN_KQTN_TD = 0;
                    if (model.DTTN_KQTN_VANG_CC == "1")
                        _model.DTTN_KQTN_CC = 0;

                    List<SXEPHANG> lstXepHang = _db.Filter<SXEPHANG>(o => o.XEPHANG_HANGGPLX_MA == model.DTTN_HGPLX_MA);
                    if (lstXepHang != null && lstXepHang.Count > 0)
                    {
                        int k = 0;
                        for (int i = 0; i < lstXepHang.Count; i++)
                        {
                            if (lstXepHang[i].XEPHANG_DIEMTB_DEN > model.DTTN_KQTN_TKKH)//sửa 2017-08-20
                            {
                                _model.DTTN_KQTN_ID_XEPHANG = lstXepHang[i].ID;
                                k = i;
                                break;
                            }

                        }
                        if (_model.DTTN_KQTN_ID_XEPHANG == null)
                        {
                            _model.DTTN_KQTN_ID_XEPHANG = lstXepHang[lstXepHang.Count - 1].ID;
                        }

                        //Kiểm tra chi tiết từng nội dung thi:
                        if (_model.DTTN_KQTN_LT < lstXepHang[0].XEPHANG_LTMIN && (_model.DTTN_KQTN_TD > lstXepHang[0].XEPHANG_TDMIN && _model.DTTN_KQTN_TH > lstXepHang[0].XEPHANG_THMIN && _model.DTTN_KQTN_CC > lstXepHang[0].XEPHANG_CCMIN))
                        {
                            _model.DTTN_KQTN_TTTN = "TLT";
                        }
                        else if (_model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN)
                        {
                            _model.DTTN_KQTN_TTTN = "TTH";
                        }
                        else if (_model.DTTN_KQTN_LT < lstXepHang[0].XEPHANG_LTMIN && (_model.DTTN_KQTN_TD < lstXepHang[0].XEPHANG_TDMIN || _model.DTTN_KQTN_TH < lstXepHang[0].XEPHANG_THMIN || _model.DTTN_KQTN_CC < lstXepHang[0].XEPHANG_CCMIN))
                        {
                            _model.DTTN_KQTN_TTTN = "TTN";
                        }

                        if (_model.DTTN_KQTN_LT >= lstXepHang[0].XEPHANG_LTMIN && _model.DTTN_KQTN_TD >= lstXepHang[0].XEPHANG_TDMIN
                             && _model.DTTN_KQTN_TH >= lstXepHang[0].XEPHANG_THMIN && _model.DTTN_KQTN_CC >= lstXepHang[0].XEPHANG_CCMIN
                             && lstXepHang[k].XEPHANG_ACTIVE == "1")
                        {
                            _model.DTTN_KQTN_TTTN = "DAT";
                        }

                        if (model.DTTN_KQTN_VANG_LT == "1")
                            _model.DTTN_KQTN_LT = null;
                        if (model.DTTN_KQTN_VANG_TH == "1")
                            _model.DTTN_KQTN_TH = null;
                        if (model.DTTN_KQTN_VANG_TD == "1")
                            _model.DTTN_KQTN_TD = null;
                        if (model.DTTN_KQTN_VANG_CC == "1")
                            _model.DTTN_KQTN_CC = null;

                    }



                    _db.Update<DDTTN_KQTN>(_model);

                    //DHV_HS
                    var _DHV_HS = _db.GetOne<DHV_HS>(o => o.HV_HS_DVGTVT_MA == model.DTTN_KQTN_DVGTVT_MA && o.HV_HS_HV_MA == model.DTTN_KQTN_HV_MA);
                    if (_DHV_HS != null)
                    {


                        _DHV_HS.HV_HS_TT = _model.DTTN_KQTN_TTTN;


                        _db.Update<DHV_HS>(_DHV_HS);
                    }
                }
                return Json(new { status = true, id = _ID, message = "" });
            }
            catch (Exception ex)
            {
                return Json(new { status = false, id = 0, message = ex.ToString() });
            }


        }

        #region detail
        public async Task<ActionResult> Detail(string id1, string id2, string id3, string id4)
        {
            DDTTN_KQTN model = await _db.GetDbSet<DDTTN_KQTN>().FindAsync(id1, id2, id3, id4);
            CrudModelDDTTN_KQTN _model = new CrudModelDDTTN_KQTN();
            GPS.COMMON.Helpers.CopyObject<CrudModelDDTTN_KQTN>(model, ref _model);
            ViewBag.SSHVs = new SelectList(_db.GetAll<SSHV>(), "SHV_MA", "SHV_HOTEN");
            string HV_MA = id4;
            string HV_DVGTVT = id1;
            DHV _hocvien = _db.GetOne<DHV>(o => o.HV_MA == HV_MA && o.HV_DVGTVT_MA == HV_DVGTVT);
            ViewBag.TEN_HV = _hocvien.HV_HODEM + " " + _hocvien.HV_TEN;

            return PartialView("_Detail", _model);
        }

        #endregion

        #region "function"

        #endregion
    }
}